/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class Related
    {
        private System.Xml.XmlElement m_XmlElement;

        private Microsoft.CSA.FeedSync.FeedSyncLibrary.Sharing m_Sharing;

        public enum RelatedTypes
        {
            Complete = 0,
            Aggregated = 1
        }

        private string m_Link;
        private string m_Title;

        private Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes m_RelatedType;

        public Related(Microsoft.CSA.FeedSync.FeedSyncLibrary.Sharing i_Sharing, System.Xml.XmlElement i_RelatedXmlElement)
        {
            if (i_Sharing == null)
                throw new System.ArgumentException("i_Sharing must be non-null!");

            if (i_RelatedXmlElement == null)
                throw new System.ArgumentException("i_Related.XmlElement must be non-null!");

            bool InvalidXmlElement =
                (i_RelatedXmlElement.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_ELEMENT_NAME) ||
                (i_RelatedXmlElement.NamespaceURI != Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_URI);

            if (InvalidXmlElement)
                throw new System.ArgumentException("Invalid i_RelatedXmlElement!");

            m_Sharing = i_Sharing;
            m_XmlElement = i_RelatedXmlElement;

            string RelatedType = i_RelatedXmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.TYPE_ATTRIBUTE);
            switch (RelatedType)
            {
                case Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_TYPE_AGGREGATED:
                    {
                        m_RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes.Aggregated;
                        break;
                    }

                case Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_TYPE_COMPLETE:
                    {
                        m_RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes.Complete;
                        break;
                    }

                default:
                    {
                        throw new System.ArgumentException("Unknown related node type: " + RelatedType);
                    }
            }

            m_Link = i_RelatedXmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.LINK_ATTRIBUTE);

            if (i_RelatedXmlElement.HasAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.TITLE_ATTRIBUTE))
                m_Title = i_RelatedXmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.TITLE_ATTRIBUTE);
        }

        public Related(Microsoft.CSA.FeedSync.FeedSyncLibrary.Sharing i_Sharing, Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes i_RelatedType,  string i_Link, string i_Title)
        {
           if (i_Sharing == null)
                throw new System.ArgumentException("i_Sharing must be non-null!");

            if (System.String.IsNullOrEmpty(i_Link))
                throw new System.ArgumentException("i_Link must be non-null!");

            string RelatedType = System.String.Empty;
            switch (m_RelatedType)
            {
                case Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes.Aggregated:
                    {
                        RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_TYPE_AGGREGATED;
                        break;
                    }

                case Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes.Complete:
                    {
                        RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_TYPE_COMPLETE;
                        break;
                    }

                default:
                    {
                        throw new System.ArgumentException("Unknown i_RelatedType!");
                    }
            }

            string ElementName = System.String.Format
                (
                "{0}:{1}",
                i_Sharing.NamespacePrefix,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_ELEMENT_NAME
                );

            m_XmlElement = i_Sharing.XmlElement.OwnerDocument.CreateElement
                (
                ElementName,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_PREFIX
                );

            m_Sharing = i_Sharing;
            m_RelatedType = i_RelatedType;
            m_Link = i_Link;
            m_Title = i_Title;

            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.TYPE_ATTRIBUTE, RelatedType);
            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.LINK_ATTRIBUTE, m_Link);

            if (!System.String.IsNullOrEmpty(m_Title))
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.TITLE_ATTRIBUTE, i_Title);
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrary.Sharing Sharing
        {
            get
            {
                return m_Sharing;
            }
        }

        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrary.Related.RelatedTypes RelatedType
        {
            get
            {
                return m_RelatedType;
            }
        }

        public string Link
        {
            get
            {
                return m_Link;
            }
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
        }

    }
}
